package com.zwg.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.zwg.tingshu.album.mapper.*;
import com.zwg.tingshu.album.mapper.*;
import com.zwg.tingshu.album.service.BaseCategoryService;
import com.zwg.tingshu.common.cache.GuiGuCache;
import com.zwg.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwg.tingshu.model.album.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	/**
	 * 根据1级分类对象查询包含二级分类（包含三级分类）
	 *
	 * @param category1Id
	 * @return
	 */
	@Override
	@GuiGuCache(prefix = "baseCategoryListByCategory1Id:")
	public JSONObject getBaseCategoryListByCategory1Id(Long category1Id) {
		//1.根据1级分类ID查询分类视图得到一级分类列表
		LambdaQueryWrapper<BaseCategoryView> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BaseCategoryView::getCategory1Id, category1Id);
		List<BaseCategoryView> baseCategory1List = baseCategoryViewMapper.selectList(queryWrapper);
		//2.处理一级分类对象 封装一级分类对象包含ID，分类名称
		if (CollectionUtil.isNotEmpty(baseCategory1List)) {
			//2.1 构建一级分类对象 封装ID，名称
			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("categoryId", baseCategory1List.get(0).getCategory1Id());
			jsonObject1.put("categoryName", baseCategory1List.get(0).getCategory1Name());
			//3.处理一级分类下二级分类
			//3.1 将一级分类集合再按照二级分类ID分组得到Map Map中key:二级分类ID，Map中Value二级分类集合
			Map<Long, List<BaseCategoryView>> category2Map = baseCategory1List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//3.2 遍历Map每遍历一次Map封装二级分类JSON对象
			List<JSONObject> jsonObject2List = new ArrayList<>();
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
				//3.3 构建二级分类对象，封装二级分类ID及名称
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", entry2.getKey());
				jsonObject2.put("categoryName", entry2.getValue().get(0).getCategory2Name());
				jsonObject2List.add(jsonObject2);
				//4.处理二级分类下三级分类
				//4.1 遍历二级分类列表，没遍历一条记录构建三级分类对象
				List<JSONObject> jsonObject3List = new ArrayList<>();
				for (BaseCategoryView baseCategoryView : entry2.getValue()) {
					//4.2 构建三级分类对象，封装三级分类ID名称
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
					jsonObject3List.add(jsonObject3);
				}
				//4.3 将三级分类集合放入二级分类对象中
				jsonObject2.put("categoryChild", jsonObject3List);
			}
			//3.3 将二级分类集合放入一级分类对象中
			jsonObject1.put("categoryChild", jsonObject2List);
			return jsonObject1;
		}
		return null;
	}


	/**
	 * 查询指定1级分类下置顶前7个三级分类集
	 *
	 * @param category1Id 1级分类ID
	 * @return
	 */
	@Override
	@GuiGuCache(prefix = "topBaseCategory3:")
	public List<BaseCategory3> getTop7BaseCategory3(Long category1Id) {
		//1.根据一级分类ID查询二级分类集合，得到二级分类ID集合
		LambdaQueryWrapper<BaseCategory2> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BaseCategory2::getCategory1Id, category1Id);
		queryWrapper.select(BaseCategory2::getId);
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(queryWrapper);
		if (CollectionUtil.isNotEmpty(baseCategory2List)) {
			List<Long> baseCategory2IdList = baseCategory2List.stream().map(baseCategory2 -> baseCategory2.getId()).collect(Collectors.toList());
			//2.根据二级分类ID集合查询三级分类列表获取置顶前7个三级分类集合
			LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<>();
			//2.1 查询二级分类下包含三级分类
			baseCategory3LambdaQueryWrapper.in(BaseCategory3::getCategory2Id, baseCategory2IdList);
			//2.2 查询需要置顶的三级分类
			baseCategory3LambdaQueryWrapper.eq(BaseCategory3::getIsTop, 1);
			//2.3 根据序号排序 升序排
			baseCategory3LambdaQueryWrapper.orderByAsc(BaseCategory3::getOrderNum);
			//2.4 截取前7个 SQL+limit 7
			baseCategory3LambdaQueryWrapper.last("limit 7");
			return baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);
		}
		return null;
	}


	/**
	 * 获取所有分类
	 * @return
	 */
	@Override
	@GuiGuCache(prefix = "baseCategoryList:")
	public List<JSONObject> getBaseCategoryList() {
		List<JSONObject> allList = new ArrayList<>();
		//1. 查询所有分类
		List<BaseCategoryView> allCategoryList = baseCategoryViewMapper.selectList(null);
		// 2.处理一级分类
		Map<Long, List<BaseCategoryView>> category1Map = allCategoryList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		if(CollectionUtil.isNotEmpty(category1Map)){
			for (Map.Entry<Long, List<BaseCategoryView>> entry1 : category1Map.entrySet()) {
				// 1 创建一个一级分类的JSON对象
				JSONObject jsonObject1 = new JSONObject();
				// 2.1 获取一级分类id和一级分类名称
				Long catefory1Id = entry1.getKey();
				String category1Name = entry1.getValue().get(0).getCategory1Name();
				jsonObject1.put("categoryId", catefory1Id);
				jsonObject1.put("categoryName", category1Name);
				// 3 处理二级分类
				Map<Long, List<BaseCategoryView>> category2Map = entry1.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
				if (CollectionUtil.isNotEmpty(category2Map)) {
					List<JSONObject> jsonObject2List = new ArrayList<>();
					for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
						JSONObject jsonObject2 = new JSONObject();
						// 3.1 获取二级分类id和二级分类名称
						Long catefory2Id = entry2.getKey();
						String category2Name = entry2.getValue().get(0).getCategory2Name();
						jsonObject2.put("categoryId", catefory2Id);
						jsonObject2.put("categoryName", category2Name);
						// 4 处理三级分类
						Map<Long, List<BaseCategoryView>> category3Map = entry2.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
						if (CollectionUtil.isNotEmpty(category3Map)) {
							List<JSONObject> jsonObject3List = new ArrayList<>();
							for (Map.Entry<Long, List<BaseCategoryView>> entry3 : category3Map.entrySet()) {
								JSONObject jsonObject3 = new JSONObject();
								// 4.1 获取三级分类id和三级分类名称
								Long catefory3Id = entry3.getKey();
								String category3Name = entry3.getValue().get(0).getCategory3Name();
								jsonObject3.put("categoryId", catefory3Id);
								jsonObject3.put("categoryName", category3Name);
								jsonObject3List.add(jsonObject3);
							}
							// 4.2 将三级分类添加到二级分类中
							jsonObject2.put("categoryChild", jsonObject3List);
						}
						jsonObject2List.add(jsonObject2);
					}
					// 3.2 将二级分类添加到一级分类中
					jsonObject1.put("categoryChild", jsonObject2List);
				}
				// 2.2 将一级分类添加到最终结果中
				allList.add(jsonObject1);
			}
		}
		return allList;
	}

	/**
	 * 根据一级分类id获取属性
	 * @param category1Id 一级分类id
	 * @return 属性列表
	 */
	@Override
	@GuiGuCache(prefix = "category1Id:attributes:")
	public List<BaseAttribute> getAttributesByCategory1Id(Long category1Id) {
		return baseAttributeMapper.getAttributesByCategory1Id(category1Id);
	}

	/**
	 * 根据三级分ID（专辑视图主键）查询专辑信息
	 *
	 * @param category3Id
	 * @return
	 */
	@Override
	@GuiGuCache(prefix = "category3View:")
	public BaseCategoryView getCategoryViewBy3Id(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}
}
